package com.whut.whw.personalinfo;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.whut.whw.R;
import com.whut.whw.util.GlideCircularLoadUtils;

import java.util.List;

/**
 * Created by 曾旺红 on 2017/06/22.
 */

public class FollowListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<FollowListModel> list;
    public static Context mcontext;

    //点击
    private OnItemClickListener mOnItemClickListener;
    //长按
    private OnItemLongClickListener mOnItemLongClickListener;
    //点击
    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
    //长按
    public void setmOnItemLongClickListener(OnItemLongClickListener mOnItemLongClickListener) {
        this.mOnItemLongClickListener = mOnItemLongClickListener;
    }
    //点击监听接口
    public interface OnItemClickListener{
        void onItemClick(View view, int position);
    }
    //长按监听接口
    public interface OnItemLongClickListener{
        void onItemLongClick(View view, int position);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView othimage;
        TextView othname;
        TextView othintro;
        TextView othcredit;
        LinearLayout othinfo;
        LinearLayout contact;

        public ViewHolder(View itemView) {
            super(itemView);
            othimage=(ImageView) itemView.findViewById(R.id.other_photo);
            othname=(TextView)itemView.findViewById(R.id.other_name);
            othintro=(TextView)itemView.findViewById(R.id.other_intro);
            othcredit=(TextView)itemView.findViewById(R.id.other_credit);
            othinfo=(LinearLayout)itemView.findViewById(R.id.other_info);
            contact=(LinearLayout)itemView.findViewById(R.id.talk);
        }
    }

    public FollowListAdapter(List<FollowListModel> list)
    {
        this.list=list;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mcontext=parent.getContext();
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_follow_item,parent,false);
        RecyclerView.ViewHolder holder=new ViewHolder(view);
        return holder;
    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        FollowListModel follow=list.get(position);
        Glide
                .with(mcontext)
                .load(follow.getImage())
                .asBitmap().centerCrop()
                .into(new GlideCircularLoadUtils(mcontext,((ViewHolder)holder).othimage));
        ((ViewHolder)holder).othname.setText(follow.getName());
        ((ViewHolder)holder).othintro.setText(follow.getIntro());
        ((ViewHolder)holder).othcredit.setText(follow.getCredit());

        //判断是否设置了监听器
        //单击判断
        if(mOnItemClickListener != null){
            //为ItemView设置监听器
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(holder.itemView,position);
                }
            });
        }
        //长按判断
        if(mOnItemLongClickListener != null){
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int position = holder.getLayoutPosition();
                    mOnItemLongClickListener.onItemLongClick(holder.itemView,position);
                    //返回true 表示消耗了事件 事件不会继续传递
                    return true;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
